package com.gov.hsams.controller;




import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.server.HttpServerRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gov.hsams.common.dto.CompanyInfoDto;
import com.gov.hsams.common.dto.CompanyInfoRDto;
import com.gov.hsams.common.lang.Result;
import com.gov.hsams.entity.CheckResult;
import com.gov.hsams.entity.CompanyInfo;
import com.gov.hsams.service.*;
import com.gov.hsams.shiro.AccountProfile;
import com.gov.hsams.util.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author the fourth group
 * @since 2021-07-26
 */
@RestController
@RequestMapping("/info")
@MapperScan("com.gov.hsams.mapper")
public class CompanyInfoController {
    @Autowired
    CompanyCorpService companyCorpService;
    @Autowired
    CompanyScopeService companyScopeService;
    @Autowired
    CompanyTypeService companyTypeService;
    @Autowired
    CompanyInfoService companyInfoService;
    @Autowired
    CheckResultService checkResultService;

//    /**
//     * 这是申请后公司信息录入到数据库
//     * @param companyinfodto
//     * @return
//     */
//    @RequiresAuthentication
//    @PostMapping("/inser")
//    public Result inserComInfor(@Validated @RequestBody CompanyInfoDto companyinfodto){
//        CompanyInfo temp = new CompanyInfo();
//        //法人ID
//        int i = companyCorpService.findCorpId(companyinfodto.getNo());
//        System.out.println(i+"这是测试");
//        temp.setCorpId(i);
////        范围ID
//        temp.setScopeId(companyScopeService.findScopeId(companyinfodto.getScope()));
//        //企业类别ID
//        temp.setTypeId(companyTypeService.findTypeId(companyinfodto.getType()));
//
////        if(companyInfoDto.getId()!=null){
////            //这是修改，并且只能修改自己的信息
////            temp = companyInfoService.getById(companyInfoDto.getId());
////            Assert.isTrue(temp.getId()== ShiroUtil.getProfile().getId(),"没有权限编辑");
////
////        }
////        else
////            //现在是添加信息
////            temp = new CompanyInfo();
////            //temp.setAddress()
////        }
//        BeanUtil.copyProperties(companyinfodto,temp,"ID","corp_id","type_id","scope_id","sanitar");
//        companyInfoService.saveOrUpdate(temp);
//        return Result.success(temp);
//    }

    /**
     * 这是根据当然登录用户的ID进行查询
     * @param
     * @return 返回公司信息
     */

    @RequiresAuthentication
    @GetMapping("/find")
    public Result find(){
        CompanyInfoRDto companyInfoRDto = new CompanyInfoRDto();
        AccountProfile accountProfile = ShiroUtil.getProfile();
        int corpId = accountProfile.getId();

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("corp_id",corpId);
        CompanyInfo companyInfo = companyInfoService.getOne(wrapper);
        //公司名称
        companyInfoRDto.setName(companyInfo.getName());
        //地址
        companyInfoRDto.setAddress(companyInfo.getAddress());
        //法人姓名
        companyInfoRDto.setCorpName(accountProfile.getName());
        companyInfoRDto.setNo(accountProfile.getNo());
        //范围
        if(companyInfo.getTypeId()==1)
            companyInfoRDto.setScope("餐饮");
        if(companyInfo.getTypeId()==2)
            companyInfoRDto.setScope("医疗器械");
        if(companyInfo.getTypeId()==3)
            companyInfoRDto.setScope("诊所");
        //职工人数
        companyInfoRDto.setPayroll(companyInfo.getPayroll());
        //面积
        companyInfoRDto.setArea(companyInfo.getArea());
        //营业执照
        companyInfoRDto.setTrade("已上传");
        QueryWrapper<CheckResult> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("company_id",companyInfo.getId());
        wrapper1.eq("classify",0);
        CheckResult checkResult = checkResultService.getOne(wrapper1);
        if(checkResult == null)
            companyInfoRDto.setChusheng("等待");
        else
        {
            if(checkResult.getCheckResult() ==1 )
                companyInfoRDto.setChusheng("通过");
            else
                companyInfoRDto.setChusheng("不通过需要整改");
        }
        wrapper1.eq("classify",1);
        checkResult = checkResultService.getOne(wrapper1);
        if(checkResult == null)
            companyInfoRDto.setFusheng("等待");
        else
        {
            if(checkResult.getCheckResult() ==1 )
                companyInfoRDto.setFusheng("通过");
            else
                companyInfoRDto.setFusheng("不通过需要整改");
        }
        wrapper1.eq("classify",2);
        checkResult = checkResultService.getOne(wrapper1);
        if(checkResult == null)
            companyInfoRDto.setZhongsheng("等待");
        else
        {
            if(checkResult.getCheckResult() ==1 )
                companyInfoRDto.setZhongsheng("通过");
            else
                companyInfoRDto.setZhongsheng("不通过需要整改");
        }
        return Result.success(companyInfoRDto);
    }
}
